package com.macro.mall.portal.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.dto.*;
import com.macro.mall.mapper.BmsMuseumApplicantMapper;
import com.macro.mall.mapper.BmsMuseumGroupMapper;
import com.macro.mall.mapper.BmsMuseumMapper;
import com.macro.mall.model.*;
import com.macro.mall.portal.dto.ActivictyApplicantDetail;
import com.macro.mall.portal.service.BmsMuseumApplicantService;
import com.macro.mall.portal.service.BmsMuseumGroupService;
import com.macro.mall.portal.service.BmsMuseumService;
import com.macro.mall.portal.util.AdminUtils;
import com.macro.mall.portal.util.MemberUtils;
import com.macro.mall.query.BmsMuseumGroupQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.*;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class BmsMuseumGroupServiceImpl implements BmsMuseumGroupService {

    @Autowired
    private BmsMuseumGroupMapper baseMapper;

    @Autowired
    private BmsMuseumApplicantService bmsMuseumApplicantService;

    @Autowired
    private BmsMuseumApplicantMapper bmsMuseumApplicantMapper;

    @Autowired
    private BmsMuseumMapper bmsMuseumMapper;
    @Autowired
    private BmsMuseumService bmsMuseumService;

    @Override
    public List<BmsMuseumGroup> findList(BmsMuseumGroupQuery query) {
        Integer pageNum = query.getPageNum();
        Integer pageSize = query.getPageSize();
        if (Objects.nonNull(pageNum) && Objects.nonNull(pageSize)) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<BmsMuseumGroup> list = baseMapper.findList(query);
        return list;
    }

    @Override
    public int create(BmsMuseumGroupParam BmsMuseumGroupParam) {
        BmsMuseumGroup BmsMuseumGroup = new BmsMuseumGroup();
        BeanUtils.copyProperties(BmsMuseumGroupParam, BmsMuseumGroup);
        AdminUtils.setCreation(BmsMuseumGroup);
        return baseMapper.insert(BmsMuseumGroup);
    }

    @Override
    public int update(BmsMuseumGroupParam param) {
        BmsMuseumGroup entity = baseMapper.getById(param.getId());
        BeanUtils.copyProperties(param, entity);
        AdminUtils.setEdition(entity);
        return baseMapper.updateById(entity);
    }

    @Override
    public BmsMuseumGroup getById(Long id) {
        return baseMapper.getById(id);
    }

    @Override
    public int delete(List<Long> ids) {
        return baseMapper.updateDeleteId(ids);
    }

    @Override
    public List<ActivictyApplicantDetail> getCurrentDataApplicantDetail(BmsMuseumGroupParam bmsMuseumGroupParam) {

        List<BmsMuseumGroup> groupList = baseMapper.getByMuseumId(bmsMuseumGroupParam.getMuseumId());

        List<BmsMuseumApplicant> groupApplicantList = bmsMuseumApplicantService.findGroupApplicantByMuseumId(bmsMuseumGroupParam);
        Map<Long, List<BmsMuseumApplicant>> groupApplicantMap = groupApplicantList.parallelStream().collect(Collectors.groupingBy(BmsMuseumApplicant::getMuseumGroupId, Collectors.toList()));

        ActivictyApplicantDetail detail;
        List<ActivictyApplicantDetail> result = new ArrayList<>();
        LocalTime currentTime = LocalTime.now();
        LocalDateTime currentDateTime = bmsMuseumGroupParam.getCurrentTime();
        for (BmsMuseumGroup bmsMuseumGroup : groupList) {
            detail = new ActivictyApplicantDetail();
            detail.setGroupId(bmsMuseumGroup.getId());
            detail.setTimePeriod(bmsMuseumGroup.getOpeningTime() + "~" + bmsMuseumGroup.getClosingTime());
            if (Objects.isNull(bmsMuseumGroup.getPeopleNumber()) || bmsMuseumGroup.getPeopleNumber() == 0) {
                detail.setRemainderTicket(0);
            }else {
                List<BmsMuseumApplicant> currentDayApplicantList = groupApplicantMap.getOrDefault(bmsMuseumGroup.getId(), new ArrayList<>());
                detail.setRemainderTicket(bmsMuseumGroup.getPeopleNumber() - currentDayApplicantList.size());
            }
            detail.setDisable(currentDateTime.toLocalDate().isEqual(LocalDate.now()) && currentTime.isAfter(bmsMuseumGroup.getClosingTime()) ? 1 : 0);
            result.add(detail);
        }
        return result;
    }

    @Override
    public List<BmsMuseumGroup> getOpenTimeByMuseumId(BmsMuseumGroupParam bmsMuseumGroupParam){
        List<BmsMuseumGroup> groupList = baseMapper.getByMuseumId(bmsMuseumGroupParam.getMuseumId());
        return groupList;
    }

    @Override
    public List<BmsMuseumGroupDetail> getReserveProjectActivities(BmsMuseumGroupParam bmsMuseumGroupParam) {
        UmsMember user = new UmsMember();
        user.setId(17L);
        BmsMuseumApplicantExample example = new BmsMuseumApplicantExample();
        example.createCriteria().andUserIdEqualTo(user.getId()).andIsDeleteEqualTo("0").andMuseumTeamIdIsNull();
        // 所有报名详情
        List<BmsMuseumApplicant> bmsMuseumApplicants = bmsMuseumApplicantMapper.selectByExample(example);

        Map<Long, Map<LocalDate, Map<String, List<BmsMuseumApplicant>>>> registrationGroup = bmsMuseumApplicants.parallelStream().collect(Collectors.groupingBy(BmsMuseumApplicant::getMuseumGroupId
                , Collectors.groupingBy(i -> i.getApplicationTime().toLocalDate(), Collectors.groupingBy(BmsMuseumApplicant::getApplicationType, Collectors.toList()))));

        BmsMuseumExample museumExample = new BmsMuseumExample();
        museumExample.createCriteria().andIsDeleteEqualTo((byte) 0);
        List<BmsMuseum> bmsMuseums = bmsMuseumMapper.selectByExample(museumExample);
        Map<Long, BmsMuseum> museumMap = bmsMuseums.parallelStream().collect(Collectors.toMap(BmsMuseum::getId, z -> z));
        List<BmsMuseumGroup> list = baseMapper.findList(null);
        Map<Long, BmsMuseumGroup> groupAndMuseumRelationMap = list.parallelStream().collect(Collectors.toMap(BmsMuseumGroup::getId, z -> z));
        BmsMuseumGroup museumGroup;
        BmsMuseum museum;
        List<BmsMuseumGroupDetail> resultList = new ArrayList<>();
        BmsMuseumGroupDetail bmsMuseumGroupDetail;
        for (Map.Entry<Long, Map<LocalDate, Map<String, List<BmsMuseumApplicant>>>> groupIdMapEntry : registrationGroup.entrySet()) {
            museumGroup = groupAndMuseumRelationMap.getOrDefault(groupIdMapEntry.getKey(), null);
            if (Objects.isNull(museumGroup)) {
                continue;
            }

            museum = museumMap.getOrDefault(museumGroup.getMuseumId(), null);
            if (Objects.isNull(museum)) {
                continue;
            }
            for (Map.Entry<LocalDate, Map<String, List<BmsMuseumApplicant>>> localDateMapEntry : groupIdMapEntry.getValue().entrySet()) {
                for (Map.Entry<String, List<BmsMuseumApplicant>> applicantMapEntry : localDateMapEntry.getValue().entrySet()) {
                    bmsMuseumGroupDetail = new BmsMuseumGroupDetail();
                    BeanUtils.copyProperties(museumGroup, bmsMuseumGroupDetail);
                    bmsMuseumGroupDetail.setMuseumId(museum.getId());
                    bmsMuseumGroupDetail.setMuseumName(museum.getName());
                    bmsMuseumGroupDetail.setApplicantTime(localDateMapEntry.getKey().atStartOfDay());
                    bmsMuseumGroupDetail.setApplicationType(applicantMapEntry.getKey());
                    bmsMuseumGroupDetail.setTimePeriod(museumGroup.getOpeningTime() + "~" + museum.getClosingTime());
                    bmsMuseumGroupDetail.setApplicantCount(applicantMapEntry.getValue().size());
                    resultList.add(bmsMuseumGroupDetail);
                }
            }

        }
        return resultList;
    }

    @Override
    public BmsMuseumGroupDetail getRegistrationActivitiesDetail(BmsMuseumGroupParam bmsMuseumGroupParam) {

        // 获取场馆时间段
        BmsMuseumGroup bmsMuseumGroup = baseMapper.getById(bmsMuseumGroupParam.getId());

        // 获取场馆信息
        BmsMuseumDetail bmsMuseumDetail = bmsMuseumService.getItem(bmsMuseumGroup.getMuseumId());

        UmsMember user = MemberUtils.getUser();
        BmsMuseumApplicantExample example = new BmsMuseumApplicantExample();
        example.createCriteria().andUserIdEqualTo(user.getId()).andMuseumGroupIdEqualTo(bmsMuseumGroupParam.getId()).andIsDeleteEqualTo("0").andMuseumTeamIdIsNull();
        // 所有报名详情
        List<BmsMuseumApplicant> bmsMuseumApplicants = bmsMuseumApplicantMapper.selectByExample(example);
        bmsMuseumApplicants = bmsMuseumApplicants.parallelStream().filter(i -> i.getApplicationTime().toLocalDate().equals(bmsMuseumGroupParam.getApplicantTime().toLocalDate())).collect(Collectors.toList());

        ApplicantDetail applicantDetail;
        List<ApplicantDetail> applicantDetails = new ArrayList<>();
        for (BmsMuseumApplicant bmsMuseumApplicant : bmsMuseumApplicants) {
            applicantDetail = new ApplicantDetail();
            applicantDetail.setName(bmsMuseumApplicant.getUserName());
            applicantDetail.setPhone(bmsMuseumApplicant.getUserPhone());
            applicantDetail.setIdCardNo(bmsMuseumApplicant.getUserIdCard());
            applicantDetail.setIdCardType(bmsMuseumApplicant.getUserIdType().toString());
            applicantDetails.add(applicantDetail);
        }
        BmsMuseumGroupDetail bmsMuseumGroupDetail = new BmsMuseumGroupDetail();
        BeanUtils.copyProperties(bmsMuseumGroup, bmsMuseumGroupDetail);
        bmsMuseumGroupDetail.setMuseumName(bmsMuseumDetail.getName());
        bmsMuseumGroupDetail.setPicList(bmsMuseumDetail.getPicList());
        bmsMuseumGroupDetail.setCoverPic(bmsMuseumDetail.getCoverPic());
        bmsMuseumGroupDetail.setTimePeriod(bmsMuseumGroup.getOpeningTime() + "~" + bmsMuseumGroup.getClosingTime());
        bmsMuseumGroupDetail.setApplicantCount(applicantDetails.size());
        bmsMuseumGroupDetail.setApplicantUserList(applicantDetails);

        return bmsMuseumGroupDetail;
    }
}
